/**
 * @(#)com.tansun.easycare.rule.excel.handle.impl.EvaluationIndexHandle.java Copyright (c) 2017
 * 天阳宏业科技股份有限公司
 */
package com.tansun.easycare.rule.excel.handle.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import com.tansun.easycare.rule.excel.entity.decision.DecisionExcelEntity;
import com.tansun.easycare.rule.excel.handle.BaseHandle;
import com.tansun.easycare.rule.excel.utils.ExcelUitl;
import com.tansun.easycare.rule.referencedata.entity.RuleReferenceData;
import com.tansun.easycare.rule.table.entity.RuleDecisiontableDetail;

/**
 * 赋值索引处理类
 * @author zengdong
 * @version 1.0  2017年5月27日
 * @modified zengdong  2017年5月27日  <创建>
 */
public class EvaluationIndexHandle extends BaseHandle<DecisionExcelEntity> {

    @Override
    public List<DecisionExcelEntity> excelToEntity(Workbook wb, String projectCode,String userId) {
        DecisionExcelEntity entity = new DecisionExcelEntity();
        entity.setProjectCode(projectCode);
        Sheet sheet = wb.getSheetAt(0);
        //referenceData数组
        List<RuleReferenceData> referenceDataList = new ArrayList<RuleReferenceData>();
        //decisionDetail数组
        List<RuleDecisiontableDetail> detailList = new ArrayList<RuleDecisiontableDetail>();

        //获取最后一行行数
        int rowNum = sheet.getLastRowNum();
        Row row = getRow(sheet, 0);

        //获取总列数
        int colNum = 0;

        for (int i = 0; i < rowNum; i++) {
            row = getRow(sheet, i);
            colNum = row.getPhysicalNumberOfCells();
            //遇到{end}退出
            if (i > 1 && row.getCell(0).toString().indexOf("{end}") != -1) {
                break;
            }
            if (i == 0) {    //ruleManage标题
                entity.setName(getCell(row, 0) == null ? "" : getCell(row, 0).toString());
                continue;
            } else if (i == 1) {
                int num = 0;
                while (num < colNum) {
                    //如果遇到{#}停止
                    if (getCell(row, num).toString().indexOf("{#}") != -1) {
                        break;
                    }
                    String controlValue = row.getCell(num).toString().trim();
                    if (StringUtils.isNotEmpty(controlValue)) {
                        RuleReferenceData referenceData = new RuleReferenceData();
                        referenceData.setNamecn(controlValue);
                        referenceData.setSeq(num);
                        referenceData.setContentType(num > 0 ? "out" : "in");
                        referenceDataList.add(referenceData);
                        num++;
                    } else {
                        num++;
                        continue;
                    }
                }
            } else {
                int num = 0;
                while (num < colNum) {
                    //如果遇到{#}停止
                    if (getCell(row, num).toString().indexOf("{#}") != -1) {
                        break;
                    }
                    String controlValue = row.getCell(num).toString().trim();
                    RuleDecisiontableDetail detail = new RuleDecisiontableDetail();
                    detail.setControlValue(StringUtils.isNotBlank(controlValue) ? controlValue : "");
                    detail.setRowNo(i - 2);
                    detail.setColumnNo(num);
                    detail.setContentType(num > 0 ? "out" : "in");
                    detailList.add(detail);
                    num++;
                }
            }
        }

        entity.setReferenList(referenceDataList);
        entity.setDetailList(detailList);

        List<DecisionExcelEntity> list = new ArrayList<DecisionExcelEntity>();
        list.add(entity);
        return list;
    }

    @Override
    public Workbook entityToExcel(List<DecisionExcelEntity> list) {
        initHandleTemplate();
        XSSFSheet sheet = workbook.createSheet();
        if (list != null && list.size() > 0) {
            DecisionExcelEntity entity = list.get(0);
            //写入首行标题
            this.createMergedCell(sheet, 0, 0, 0, 8, entity.getName());
            Cell cell = getCell(sheet, 0, 0);
            cell.setCellStyle(titleStyle);
            int maxRow = 0;
            int maxCol = 0;
            //输出第2行表头信息
            int beginNum = 0;
            List<RuleReferenceData> referenceDataList = entity.getReferenList();
            if (referenceDataList != null && referenceDataList.size() > 0) {
                Row row = getRow(sheet, 1);
                for (int i = 0; i < referenceDataList.size(); i++) {
                    cell = getCell(row, i);
                    cell.setCellValue(referenceDataList.get(i).getNamecn());
                }
            }
            //填值
            List<RuleDecisiontableDetail> detailList = entity.getDetailList();
            if (detailList != null && detailList.size() > 0) {
                for (RuleDecisiontableDetail detail : detailList) {
                    cell = getCell(sheet, detail.getRowNo() + 2, detail.getColumnNo());
                    if (detail.getRowNo() + 3 > maxRow) {
                        maxRow = detail.getRowNo() + 3;
                    }
                    if (detail.getColumnNo() + 1 > maxCol) {
                        maxCol = detail.getColumnNo() + 1;
                    }
                    cell.setCellValue(ExcelUitl.filterValue(detail.getControlValue(), (StringUtils.isNotBlank(detail.getDomain()) && "string".equals(detail.getDataType().toLowerCase())) ? "EMUN" : detail.getDataType(), detail.getContentType(), detail.getDomain(), detail.getLowerOpt(), detail.getUpperOpt()));
                }
            }
            //修饰
            for (int i = 1; i < maxRow; i++) {
                cell = getCell(sheet, i, maxCol);
                cell.setCellValue("{#}");
            }
            cell = getCell(sheet, maxRow, 0);
            cell.setCellValue("{end}");
            return workbook;
        }
        return null;
    }

}
